package com.hucsmn.examsystem.util;

/**
 * 字节数组十六进制处理工具类。
 */
public class BuildStrings {
    public static final String INDENT = "    ";
    public static final String NEWLINE = System.lineSeparator();

    private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String bytesToString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(2 * bytes.length);
        appendBytes(sb, bytes);
        return sb.toString();
    }

    public static void appendBytes(StringBuilder sb, byte[] bytes) {
        for (byte b : bytes) {
            int ub = b >= 0 ? b : b + 256;
            sb.append(HEX_DIGITS[ub / 16]);
            sb.append(HEX_DIGITS[ub % 16]);
        }
    }
}
